<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
    <%
    	String path = request.getContextPath();
    %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
	* {
		margin:0px;
		padding:0px;
	}
</style>

	
	<link rel="stylesheet" type="text/css" href="<%=path%>/js/jquery-easyui-1.4.5/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="<%=path%>/js/jquery-easyui-1.4.5/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="<%=path%>/css/admin.css">
	<script type="text/javascript" src="<%=path%>/js/jquery.js"></script>
	<script type="text/javascript" src="<%=path%>/js/jquery-easyui-1.4.5locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="<%=path%>/js/jquery-easyui-1.4.5/jquery.easyui.min.js"></script>
<script>

	function setPagination(tableId) {
		var p = $("#" + tableId).datagrid("getPager"); // 获取由tableId指定的datagrid控件的分页组件
		$(id).datagrid("load"); //跳转到第一页
		$(id).datagrid("reload"); // 刷新当前页
		$(p).pagination({
			pageList:[10, 20,30],
			beforePageText:"第",
			afterPageText:"页    共{pages}页",
			displayMsg:"当前显示{from} - {to} 条记录    共{total}条记录",
			onBeforeRefresh:function() {
				$(this).pagination("loading");
				$(this).pagination("loaded");
			}
		});
	}
	
	$(function() {
		setPagination("list");
		/**
		$(document).bind('contextmenu',function(e){ // 给网页绑定右键菜单
			e.preventDefault(); // 如果没有加这句话，则还会打开网页浏览器自带的右键菜单 。。。阻止默认行为
			$('#mm').menu('show', {
				left: e.pageX, // 鼠标右键点击的x坐标
				top: e.pageY // 鼠标右键点击的y坐标
			});
		});
		**/
		
		$("#new").click(function() {
			$.messager.alert("提示", "新建菜单", "info");
		});
		
		$("#list").datagrid({
			onRowContextMenu: function(e, rowIndex, rowData) {  // datagrid中的每一行都绑定右键菜单
                e.preventDefault(); 
                $('#mm').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }
		});
		
	});
	
	function edit() {
		var row = $("#list").datagrid("getSelected"); // 获取datagrid中被选中的行
		if (row) {
			$("#editForm").form("load", row); // .form("clear");
			$("#editWin").window("open"); // close关闭
		} else {
			$.messager.alert('提示', '请选中需要修改的产品', 'info');// messager消息控件
		}
	}
	
	function add() {
		$("#addWin").window("open"); // close关闭
	}
	
	function doEdit() {
		if ($("#editForm").form("validate")) { // 验证整个表单里的所有validatabox是否通过验证
			var id = $("#editId").val();
			var name = $("#editName").val();
			var pwd = $("#editPwd").val();
			$.post(
					'admin/adminUpdate',
					{
						"admin.aname":name,
						"admin.apwd":pwd,
						"admin.aid":id
					}, 
					function(data) {
						var d = eval("("+data+")");
						if (d.result == 'success') {
							$.messager.alert("提示", d.msg, "info", function() {
									$("#editWin").window("close");
									$("#list").datagrid("reload");
							});
						} else {
							$.messger.alert("提示", d.msg + " 请稍候再试", "info");
						}
					});
		} 
	}
	
	function doAdd() {
		if ($("#addForm").form("validate")) { // 验证整个表单里的所有validatabox是否通过验证
			var aname = $("#aname").val();
			var apwd = $("#apwd").val();
			$.post(
					'admin/adminSave',
					{
						"admin.aname":aname,
						"admin.apwd":apwd
					},
					function(data) {
						var d = eval("("+data+")");
						if (d.result == 'success') {
							$.messager.alert("提示", d.msg, "info", function() {
									$("#addWin").window("close");
									$("#addForm").form("clear");
							});
						} else {
							$.messger.alert("提示", d.msg, "info");
						}
					});
			$("#list").datagrid("reload");
		} 
	}
	
	function removePro() {
		var row = $("#list").datagrid("getSelected");
		if (row) {
			$.post("admin/adminDelete", {'admin.aid':row.aid}, function(data) {
				var d = eval("("+data+")");
				if (d.result == "success") {
					$.messager.alert("提示", d.msg, "info", function() {
						$("#list").datagrid("reload");
					});
				}
			});
		} else {
			$.messager.alert("提示", "请先选择需要移除的管理员", "warning");
		}
	}
	
</script>

</head>
<body>

<s:debug></s:debug>

	<table id="list" class="easyui-datagrid" toolbar="#tb" data-options="
		url:'<%=path %>/admin/adminQueryPager', 
		method:'get', 
		rownumbers:true,
		singleSelect:true,
		autoRowHeight: true,
		pagination:true,
		border:false,
		pageSize:20">
		<thead>
			<tr>
				<th field="aid" checkbox="true" width="100">管理员</th>
				<th field="atype" width="100">atype</th>
				<th field="aname"  width="100">aname</th>
				<th field="account"  width="100">account</th>
				<th field="apwd"  width="100">apwd</th>
			</tr>
		</thead>
	</table>
	<div id="tb" style="padding:10px;">
		<a href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="add();">添加</a>
		<!-- 链接按钮控件 -->
		<a href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onclick="edit();">修改</a>
		<a href="javascript:;" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onclick="removePro();">移除</a>
	</div>
	
	<div id="addWin" class="easyui-window" title="添加管理员" data-options="iconCls:'icon-edit', closable:true, closed:true"  style="width:300px;height:200px;padding:5px;">
		<s:form id="addForm" enctype="multipart/form-data" theme="simple" >
			<table>
				<tr>
					<td>管理员类型</td>
					<td>
						<input class="easyui-validatebox textbox" id="atype" readonly value="普通管理员" data-options="required:true, novalidate:true" />
					</td>
				</tr>
				<tr>
					<td>管理员名称</td>
					<td>
						<input class="easyui-validatebox textbox" id="aname" data-options="required:true, novalidate:true" />
					</td>
				</tr>
				<tr>
					<td>管理员密码</td>
					<td>
						<input class="easyui-validatebox textbox" id="apwd"  data-options="required:true, novalidate:true" />
					 </td>
				</tr>
				<tr>
					<td></td>
					<td>
						<input type="button" onclick="doAdd();" value="确认" />
					</td>
				</tr>
			</table>
		</s:form>
	</div>
	
	<div id="editWin" class="easyui-window" title="修改管理员" data-options="iconCls:'icon-edit', closable:true, closed:true"  style="width:300px;height:200px;padding:5px;">
		<form id="editForm" enctype="multipart/form-data">
			<table>
				<tr>
					<td><input type="hidden" class="easyui-validatebox textbox" id="editId" name="aid" /></td>
				</tr>
				<tr>
					<td>管理员类型</td>
					<td>
						<input class="easyui-validatebox textbox" name="atype" readonly value="普通管理员" data-options="required:true, novalidate:true" />
					</td>
				</tr>
				<tr>
					<td>管理员名称</td>
					<td>
						<input class="easyui-validatebox textbox" id="editName" name="aname"  data-options="required:true" /><!-- 由dataoptions指定验证的规则 -->
					</td>
				</tr>
				<tr>
					<td>管理员密码</td>
					<td>
						<input class="easyui-validatebox password" id="editPwd"  name="apwd"  data-options="required:true"/>
					</td>
				</tr>
				<tr>
					<td></td>
					<td>
						<input type="button" onclick="doEdit();" value="确认" />
					</td>
				</tr>
			</table>
		</form>
	</div>
	
	<div id="mm" class="easyui-menu" style="width:120px;">
		<div id="new" data-options="name:'new'">New</div>
		<div data-options="name:'save',iconCls:'icon-save'">Save</div>
		<div data-options="name:'print',iconCls:'icon-print'">Print</div>
		<div class="menu-sep"></div>
		<div data-options="name:'exit'">Exit</div>
	</div>
	
</body>
</html>